package com.example.Lezedata.ui.activity.home;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.goods.GoodsNameView;
import com.example.Lezedata.util.GBTimeManager;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

public class WinnersVc extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_winners_vc;
    }

    @Override
    public CharSequence title() {
        return "Winners";
    }

    private int pageStart = 0;
    private int pageSize = 20;

    private ListView listView;
    private BaseAdapter adapter;

    ArrayList<Bid.WinPrizeListResponse.WinPrizeItem> list = new ArrayList<>();
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        listView = findViewByTag("listView");
        setAdapter();
        loadData();
    }

    void loadData(){
        Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageStart(pageStart).setPageSize(pageSize).build();
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Bid.WinPrizeListResponse>() {
            @Override
            public void complete(Bid.WinPrizeListResponse value, GBError t) {
                if (t == null){
                    if (pageStart == 0){
                        list.clear();
                    }
                    if (value.getListCount() >= pageSize){
                        pageStart++;
                    }
                    list.addAll(value.getListList());
                    adapter.notifyDataSetChanged();
                }
            }
        });
        GBNetRequest.stub().winPrizeList(pageInfo,observer);
    }

    public void setAdapter() {
        this.adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Bid.WinPrizeListResponse.WinPrizeItem getItem(int position) {
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null){
                    convertView = WinnersVcCell.newInstance(mContext);
                }
                WinnersVcCell cell = (WinnersVcCell)convertView;
                cell.setItem(getItem(position));
                return convertView;
            }
        };
        listView.setAdapter(this.adapter);
    }
}